import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import index from '../pages/index';
// const index = ()=>import("../pages/index");
const report = () => import('../pages/index/report');

const person = () => import('../pages/person');
const personinfo = () => import('../pages/person/personinfo');
const about = () => import('../pages/person/about');
const purse = () => import('../pages/person/purse');
const outmoney = () => import('../pages/person/outmoney');
const cashflow = () => import('../pages/person/cashflow');
const member = () => import('../pages/person/member');
const paystate = () => import('../pages/person/paystate');

const team = () => import('../pages/team');
const share = () => import('../pages/team/share');
const teamdetial = () => import('../pages/team/teamdetial');

const login = () => import('../pages/login');
const xieyi = () => import('../pages/login/xieyi');

const mission = () => import('../pages/mission/missionindex');
const detial = () => import('../pages/mission/detial');
const steps = () => import('../pages/mission/steps');
const misssuccess = () => import('../pages/mission/misssuccess');
const missionrember = () => import('../pages/mission/missionrember');

const activeA = () => import('../pages/active/activeA');

const routes = [
  {
    path: '/',
    name: '抖赚',
    component: index,
    meta: { index: 0 },
  },
  {
    path: '/index',
    name: '抖赚',
    component: index,
    meta: { index: 0 },
  },
  {
    path: '/report',
    name: '公告',
    component: report,
    meta: { index: 5 },
  },
  {
    path: '/mission',
    name: '任务',
    component: mission,
    meta: { index: 1 },
  },
  {
    path: '/missionrember',
    name: '任务记录',
    component: missionrember,
    meta: { index: 6 },
  },
  {
    path: '/team',
    name: '团队',
    component: team,
    meta: { index: 2 },
  },
  {
    path: '/teamdetial',
    name: '团队详情',
    component: teamdetial,
    meta: { index: 7 },
  },
  {
    path: '/person',
    name: '我的',
    component: person,
    meta: { index: 3 },
  },
  {
    path: '/personinfo',
    name: '个人信息',
    component: personinfo,
    meta: { index: 8 },
  },
  {
    path: '/about',
    name: '关于我们',
    component: about,
    meta: { index: 9 },
  },
  {
    path: '/purse',
    name: '我的钱包',
    component: purse,
    meta: { index: 10 },
  },
  {
    path: '/member',
    name: '会员',
    component: member,
    meta: { index: 12 },
  },
  {
    path: '/paystate',
    name: '会员确认',
    component: paystate,
    meta: { index: 11 },
  },
  {
    path: '/outmoney',
    name: '提现',
    component: outmoney,
    meta: { index: 13 },
  },
  {
    path: '/cashflow',
    name: '资金流水',
    component: cashflow,
    meta: { index: 14 },
  },
  {
    path: '/share',
    name: '分享',
    component: share,
    meta: { index: 15 },
  },
  {
    path: '/login',
    name: '登录',
    component: login,
    meta: { index: 16 },
  },
  {
    path: '/detial',
    name: '任务详情',
    component: detial,
    meta: { index: 17 },
  },
  {
    path: '/steps',
    name: '任务提交',
    component: steps,
    meta: { index: 18 },
  },
  {
    path: '/misssuccess',
    name: '提交完成',
    component: misssuccess,
    meta: { index: 19 },
  },
  {
    path: '/xieyi',
    name: '协议',
    component: xieyi,
    meta: { index: 20 },
  },

  {
    path: '/activeA',
    name: '活动',
    component: activeA,
    meta: { index: 21 },
  },
];
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch((error) => error);
};
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  let userinfo = localStorage.getItem('userinfo');
  if (userinfo) {
    next();
  } else {
    switch (to.path) {
      case '/':
        next();
        break;
      case '/index':
        next();
        break;
      case '/report':
        next();
        break;
      case '/detial':
        next();
        break;
      case '/steps':
        next();
        break;
      case '/mission':
        next();
        break;
      case '/login':
        next();
        break;
      case '/team':
        next();
        break;
      case '/xieyi':
        next();
        break;
      case '/activeA':
        next();
        break;
      case '/share':
        next();
        break;
      default:
        next('/login');
        break;
    }
  }
});

//导航后置守卫，可以在确定导航到目标页面时再更改title
router.afterEach((to) => {
  window.document.title = to.name;
});

export default router;
